//
//  BaseSettingViewController.m
//  WeiXin
//
//  Created by 亦心 on 15/12/7.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "BaseSettingViewController.h"
#import "SettingCell.h"
#import "SettingGroup.h"
#import "SettingItem.h"
#import "SettingArrowItem.h"

@interface BaseSettingViewController ()

@end

@implementation BaseSettingViewController

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (instancetype)init{
    return [super initWithStyle:UITableViewStyleGrouped];
}

-(instancetype)initWithStyle:(UITableViewStyle)style{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.tableView setContentInset:UIEdgeInsetsMake(-20, 0, 0, 0)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.sectionHeaderHeight = 0;
    
}



#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.dataArray.count;
}

-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return [_dataArray[section] header];
}

-(NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return [_dataArray[section] footer];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [[self.dataArray[section] items]count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.isSmallCell) {
        return 44;
    }else{
        if (indexPath.section == 0 && indexPath.row == 0) {
            return 88;
        }
        return 44;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == self.dataArray.count - 1) {
        return 64;
    }
    return 0;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SettingCell *cell = [SettingCell settingCellWithTableView:tableView];
    SettingGroup *group = _dataArray[indexPath.section];
    cell.item = group.items[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    SettingItem *item = [_dataArray[indexPath.section] items][indexPath.row];
    if (item.block) {
        item.block();
    }
    
    if ([item isKindOfClass:[SettingArrowItem class]]) {
        SettingArrowItem *arrowItem = (SettingArrowItem *)item;
        UIViewController *vc = [[arrowItem.tagClass alloc] init];
        if (vc == nil) return;
        vc.title = item.title;
        [self.navigationController pushViewController:vc animated:YES];
    }
}



@end
